using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BiochipManager : MonoBehaviour
{
	public BiochipData biochipData;
	public BiochipView biochipView;

	// Use this for initialization
	void Start ()
	{
		biochipData = GameObject.Find("Biochip").GetComponent<BiochipData>();
		biochipData.lightButtons = new List<bool>();
		biochipData.mouse = GameObject.Find("Mouse");
		biochipView = GameObject.Find("Biochip").GetComponent<BiochipView>();
		for(int i = 0; i < biochipView.Lights.Count; i++)
			biochipData.lightButtons.Add(false);
		
		setDefaultOn();
	}
	
	// Update is called once per frame
	void Update ()
	{
		Camera cam = Camera.main;
		if(cam.transform.position.y == -36)
		{
			Vector3 v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20);
			v3 = cam.ScreenToWorldPoint(v3);
			biochipData.mouse.transform.position = v3;
		}

		checkPuzzle();	
	}

	void checkPuzzle()
	{
		for(int i = 0; i < biochipData.lightButtons.Count; i++)
			if(biochipData.lightButtons[i] == false)
				return;

		scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
		scm.PlayDialogue("189");
	}

	public void onButtonPress(int num)
	{
		if(num == 0)
		{
			biochipData.lightButtons[num] = !biochipData.lightButtons[num];
			biochipData.lightButtons[num + 1] = !biochipData.lightButtons[num + 1];
		}
		else if(num == 9)
		{
			biochipData.lightButtons[num - 1] = !biochipData.lightButtons[num - 1];
			biochipData.lightButtons[num] = !biochipData.lightButtons[num];
		}
		else
		{
			biochipData.lightButtons[num - 1] = !biochipData.lightButtons[num - 1];
			biochipData.lightButtons[num] = !biochipData.lightButtons[num];
			biochipData.lightButtons[num + 1] = !biochipData.lightButtons[num + 1];
		}
		biochipData.buttonPress++;
		Debug.Log(biochipData.buttonPress);
		if(biochipData.buttonPress == 5)
			checkButtonPress();
	}

	public void onButtonPress(string text)
	{
		switch(text)
		{
			case "Again":
			setDefaultOn();
			biochipView.activatePopUp();
			break;
			case "GiveUp":
			scenechangeManager scm = GameObject.Find("SceneChangeManager").GetComponent<scenechangeManager>();
			scm.PlayDialogue("190");
			break;
			default:
			return;
		}
	}

	void setDefaultOn()
	{
		biochipData.lightButtons.Clear();
		for(int i = 0; i < biochipView.Lights.Count; i++)
			biochipData.lightButtons.Add(false);

		biochipData.lightButtons[1] = true;
		biochipData.lightButtons[2] = true;
		biochipData.lightButtons[4] = true;
		biochipData.lightButtons[6] = true;
		biochipData.lightButtons[7] = true;
		biochipData.lightButtons[9] = true;
	}

	void checkButtonPress()
	{
		for(int i = 0; i < biochipData.lightButtons.Count; i++)
			if(biochipData.lightButtons[i] == false)
			{
				biochipData.buttonPress = 0;
				biochipView.activatePopUp();				
				return;
			}

		scenechangeManager scm = GameObject.Find ("SceneChangeManager").GetComponent<scenechangeManager>();
		scm.PlayDialogue("189");
	}
}